//
//  KeyFrameAnimation.m
//  CoreAnimation
//
//  Created by LG on 2018/5/17.
//  Copyright © 2018年 iqiyi. All rights reserved.
//

#import "KeyFrameAnimationVC.h"
#import "PathAnimationView.h"

@interface KeyFrameAnimationVC ()

@property (weak, nonatomic) IBOutlet UIView *viewAnimation;


@end

@implementation KeyFrameAnimationVC

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    
    PathAnimationView *view = [[PathAnimationView alloc]initWithFrame:self.view.bounds];
    [self.view addSubview:view];
    
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:@"op1" forState:UIControlStateNormal];
    [btn setBackgroundColor:[UIColor redColor]];
    btn.titleLabel.font = [UIFont systemFontOfSize:12];
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn2 setTitle:@"op2" forState:UIControlStateNormal];
    [btn2 setBackgroundColor:[UIColor redColor]];
    btn2.titleLabel.font = [UIFont systemFontOfSize:12];
    UIButton *btn3 = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn3 setTitle:@"op3" forState:UIControlStateNormal];
    [btn3 setBackgroundColor:[UIColor redColor]];
    btn3.titleLabel.font = [UIFont systemFontOfSize:12];
    UIButton *btn4 = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn4 setTitle:@"op4" forState:UIControlStateNormal];
    [btn4 setBackgroundColor:[UIColor redColor]];
    btn4.titleLabel.font = [UIFont systemFontOfSize:12];
    UIButton *btn5 = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn5 setTitle:@"op5" forState:UIControlStateNormal];
    btn5.titleLabel.font = [UIFont systemFontOfSize:12];
    [btn5 setBackgroundColor:[UIColor redColor]];
    
    
    view.options = [NSMutableArray arrayWithObjects:btn,btn2,btn3,btn4,btn5, nil];
}

- (IBAction)btnClickKeyFrame:(UIButton *)sender
{
    CAKeyframeAnimation *keyAnimation = [[CAKeyframeAnimation alloc]init];
    keyAnimation.keyPath = @"position";
    keyAnimation.values = @[[NSValue valueWithCGPoint:CGPointMake(10, 10)],
                            [NSValue valueWithCGPoint:CGPointMake(100, 200)],
                            [NSValue valueWithCGPoint:CGPointMake(30, 100)],
                            [NSValue valueWithCGPoint:CGPointMake(200, 30)]];
    keyAnimation.keyTimes = @[@.0,@.4,@.8,@.1];
    keyAnimation.duration = 3.;
    [self.viewAnimation.layer addAnimation:keyAnimation forKey:@"key"];
}

- (IBAction)btnClickPath:(UIButton *)sender
{
    CGRect bounds = self.view.bounds;
    CGFloat width = CGRectGetWidth(bounds);
    CGFloat height = CGRectGetHeight(bounds);
    
    CAKeyframeAnimation *keyAnimation = [[CAKeyframeAnimation alloc]init];
    keyAnimation.keyPath = @"position";
    UIBezierPath *bezierPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(width/2-100, height/2-100, 200, 200)];
    keyAnimation.path = bezierPath.CGPath;
    keyAnimation.duration = 3.;
    [self.viewAnimation.layer addAnimation:keyAnimation forKey:@"key"];
}

- (IBAction)btnClickRotation:(UIButton *)sender
{
    CAKeyframeAnimation *anima = [CAKeyframeAnimation animationWithKeyPath:@"transform.rotation.z"];//在这里@"transform.rotation"==@"transform.rotation.z"
    NSValue *value1 = [NSNumber numberWithFloat:-M_PI/180*4];
    NSValue *value2 = [NSNumber numberWithFloat:M_PI/180*4];
    NSValue *value3 = [NSNumber numberWithFloat:-M_PI/180*4];
    anima.values = @[value1,value2,value3];
    anima.repeatCount = MAXFLOAT;
    
    [self.viewAnimation.layer addAnimation:anima forKey:@"shakeAnimation"];
}

@end
